home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfunc4.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.0 KB  |  299 lines

  1. /* Copyright (C) 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfunc4.c,v 1.5.2.2 2000/12/03 17:08:32 alexcher Exp $ */
  20. /* PostScript language support for FunctionType 4 (PS Calculator) Functions */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "opextern.h"
  25. #include "gsfunc.h"
  26. #include "gsfunc4.h"
  27. #include "gsutil.h"
  28. #include "idict.h"
  29. #include "ifunc.h"
  30. #include "iname.h"
  31.  
  32. /*
  33.  * FunctionType 4 functions are not defined in the PostScript language.  We
  34.  * provide support for them because they are needed for PDF 1.3.  In
  35.  * addition to the standard FunctionType, Domain, and Range keys, they have
  36.  * a Function key whose value is a procedure in a restricted subset of the
  37.  * PostScript language.  Specifically, the procedure must (recursively)
  38.  * contain only integer, real, Boolean, and procedure constants (only as
  39.  * literal operands of if and and ifelse), and operators chosen from the set
  40.  * given below.  Note that names other than true and false are not allowed:
  41.  * the procedure must be 'bound'.
  42.  *
  43.  * The following list is taken directly from the PDF 1.3 documentation.
  44.  */
  45. #define XOP(zfn) int zfn(P1(i_ctx_t *))
  46. XOP(zabs); XOP(zand); XOP(zatan); XOP(zbitshift);
  47. XOP(zceiling); XOP(zcos); XOP(zcvi); XOP(zcvr);
  48. XOP(zdiv); XOP(zexp); XOP(zfloor); XOP(zidiv);
  49. XOP(zln); XOP(zlog); XOP(zmod); XOP(zmul);
  50. XOP(zneg); XOP(znot); XOP(zor); XOP(zround);
  51. XOP(zsin); XOP(zsqrt); XOP(ztruncate); XOP(zxor);
  52. XOP(zeq); XOP(zge); XOP(zgt); XOP(zle); XOP(zlt); XOP(zne);
  53. XOP(z2copy);
  54. #undef XOP
  55. typedef struct calc_op_s {
  56.     op_proc_t proc;
  57.     gs_PtCr_opcode_t opcode;
  58. } calc_op_t;
  59. static const calc_op_t calc_ops[] = {
  60.  
  61.     /* Arithmetic operators */
  62.  
  63.     {zabs, PtCr_abs},
  64.     {zadd, PtCr_add},
  65.     {zand, PtCr_and},
  66.     {zatan, PtCr_atan},
  67.     {zbitshift, PtCr_bitshift},
  68.     {zceiling, PtCr_ceiling},
  69.     {zcos, PtCr_cos},
  70.     {zcvi, PtCr_cvi},
  71.     {zcvr, PtCr_cvr},
  72.     {zdiv, PtCr_div},
  73.     {zexp, PtCr_exp},
  74.     {zfloor, PtCr_floor},
  75.     {zidiv, PtCr_idiv},
  76.     {zln, PtCr_ln},
  77.     {zlog, PtCr_log},
  78.     {zmod, PtCr_mod},
  79.     {zmul, PtCr_mul},
  80.     {zneg, PtCr_neg},
  81.     {znot, PtCr_not},
  82.     {zor, PtCr_or},
  83.     {zround, PtCr_round},
  84.     {zsin, PtCr_sin},
  85.     {zsqrt, PtCr_sqrt},
  86.     {zsub, PtCr_sub},
  87.     {ztruncate, PtCr_truncate},
  88.     {zxor, PtCr_xor},
  89.  
  90.     /* Comparison operators */
  91.  
  92.     {zeq, PtCr_eq},
  93.     {zge, PtCr_ge},
  94.     {zgt, PtCr_gt},
  95.     {zle, PtCr_le},
  96.     {zlt, PtCr_lt},
  97.     {zne, PtCr_ne},
  98.  
  99.     /* Stack operators */
  100.  
  101.     {zcopy, PtCr_copy},
  102.     {z2copy, PtCr_copy},
  103.     {zdup, PtCr_dup},
  104.     {zexch, PtCr_exch},
  105.     {zindex, PtCr_index},
  106.     {zpop, PtCr_pop},
  107.     {zroll, PtCr_roll}
  108.  
  109.     /* Special operators */
  110.  
  111.     /*{zif, PtCr_if},*/
  112.     /*{zifelse, PtCr_ifelse},*/
  113.     /*{ztrue, PtCr_true},*/
  114.     /*{zfalse, PtCr_false}*/
  115. };
  116.  
  117. /* Fix up an if or ifelse forward reference. */
  118. private void
  119. psc_fixup(byte *p, byte *to)
  120. {
  121.     int skip = to - (p + 3);
  122.  
  123.     p[1] = (byte)(skip >> 8);
  124.     p[2] = (byte)skip;
  125. }
  126.  
  127. /*
  128.  * Check a calculator function for validity, optionally storing its encoded
  129.  * representation and add the size of the encoded representation to *psize.
  130.  * Note that we arbitrarily limit the depth of procedure nesting.  pref is
  131.  * known to be a procedure.
  132.  */
  133. #define MAX_PSC_FUNCTION_NESTING 10
  134. private int
  135. check_psc_function(const ref *pref, int depth, byte *ops, int *psize)
  136. {
  137.     long i;
  138.     uint size = r_size(pref);
  139.  
  140.     for (i = 0; i < size; ++i) {
  141.     byte no_ops[1 + max(sizeof(int), sizeof(float))];
  142.     byte *p = (ops ? ops + *psize : no_ops);
  143.     ref elt, elt2, elt3;
  144.     int code;
  145.  
  146.     array_get(pref, i, &elt);
  147.     switch (r_btype(&elt)) {
  148.     case t_integer: {
  149.         int i = elt.value.intval;
  150.  
  151. #if ARCH_SIZEOF_INT < ARCH_SIZEOF_LONG
  152.         if (i != elt.value.intval) /* check for truncation */
  153.         return_error(e_rangecheck);
  154. #endif
  155.         if (i == (byte)i) {
  156.         *p = PtCr_byte;
  157.         p[1] = (byte)i;
  158.         *psize += 2;
  159.         } else {
  160.         *p = PtCr_int;
  161.         memcpy(p + 1, &i, sizeof(i));
  162.         *psize += 1 + sizeof(int);
  163.         }
  164.         break;
  165.     }
  166.     case t_real: {
  167.         float f = elt.value.realval;
  168.  
  169.         *p = PtCr_float;
  170.         memcpy(p + 1, &f, sizeof(f));
  171.         *psize += 1 + sizeof(float);
  172.         break;
  173.     }
  174.     case t_boolean:
  175.         *p = (elt.value.boolval ? PtCr_true : PtCr_false);
  176.         ++*psize;
  177.         break;
  178.     case t_name:
  179.         if (!r_has_attr(&elt, a_executable))
  180.         return_error(e_rangecheck);
  181.         name_string_ref(&elt, &elt);
  182.         if (!bytes_compare(elt.value.bytes, r_size(&elt),
  183.                    (const byte *)"true", 4))
  184.         *p = PtCr_true;
  185.         else if (!bytes_compare(elt.value.bytes, r_size(&elt),
  186.                       (const byte *)"false", 5))
  187.         *p = PtCr_false;
  188.         else
  189.         return_error(e_rangecheck);
  190.         ++*psize;
  191.         break;
  192.     case t_operator: {
  193.         int j;
  194.  
  195.         for (j = 0; j < countof(calc_ops); ++j)
  196.         if (elt.value.opproc == calc_ops[j].proc) {
  197.             *p = calc_ops[j].opcode;
  198.             ++*psize;
  199.             goto next;
  200.         }
  201.         return_error(e_rangecheck);
  202.     }
  203.     default: {
  204.         if (!r_is_proc(&elt))
  205.         return_error(e_typecheck);
  206.         if (depth == MAX_PSC_FUNCTION_NESTING)
  207.         return_error(e_limitcheck);
  208.         if ((code = array_get(pref, ++i, &elt2)) < 0)
  209.         return code;
  210.         *psize += 3;
  211.         code = check_psc_function(&elt, depth + 1, ops, psize);
  212.         if (code < 0)
  213.         return code;
  214.         /* Check for {proc} if | {proc1} {proc2} ifelse */
  215. #define R_IS_OPER(pref, proc)\
  216.   (r_btype(pref) == t_operator && r_has_attr(pref, a_executable) &&\
  217.    (pref)->value.opproc == proc)
  218.         if (R_IS_OPER(&elt2, zif)) {
  219.         if (ops) {
  220.             *p = PtCr_if;
  221.             psc_fixup(p, ops + *psize);
  222.         }
  223.         } else if (!r_is_proc(&elt2))
  224.         return_error(e_rangecheck);
  225.         else if ((code == array_get(pref, ++i, &elt3)) < 0)
  226.         return code;
  227.         else if (R_IS_OPER(&elt3, zifelse)) {
  228.         if (ops) {
  229.             *p = PtCr_if;
  230.             psc_fixup(p, ops + *psize + 3);
  231.             p = ops + *psize;
  232.             *p = PtCr_else;
  233.         }
  234.         *psize += 3;
  235.         code = check_psc_function(&elt2, depth + 1, ops, psize);
  236.         if (code < 0)
  237.             return code;
  238.         if (ops)
  239.             psc_fixup(p, ops + *psize);
  240.         } else
  241.         return_error(e_rangecheck);
  242. #undef R_IS_OPER
  243.     }
  244.     }
  245.     next:
  246.     DO_NOTHING;
  247.     }
  248.     return 0;
  249. }
  250. #undef MAX_PSC_FUNCTION_NESTING
  251.  
  252. /* Check prototype */
  253. build_function_proc(gs_build_function_4);
  254.  
  255. /* Finish building a FunctionType 4 (PostScript Calculator) function. */
  256. int
  257. gs_build_function_4(const ref *op, const gs_function_params_t * mnDR,
  258.             int depth, gs_function_t ** ppfn, gs_memory_t *mem)
  259. {
  260.     gs_function_PtCr_params_t params;
  261.     ref *proc;
  262.     int code;
  263.     byte *ops;
  264.     int size;
  265.  
  266.     *(gs_function_params_t *)¶ms = *mnDR;
  267.     params.ops.data = 0;    /* in case of failure */
  268.     params.ops.size = 0;    /* ditto */
  269.     if (dict_find_string(op, "Function", &proc) <= 0) {
  270.     code = gs_note_error(e_rangecheck);
  271.     goto fail;
  272.     }
  273.     if (!r_is_proc(proc)) {
  274.     code = gs_note_error(e_typecheck);
  275.     goto fail;
  276.     }
  277.     size = 0;
  278.     code = check_psc_function(proc, 0, NULL, &size);
  279.     if (code < 0)
  280.     goto fail;
  281.     ops = gs_alloc_string(mem, size + 1, "gs_build_function_4(ops)");
  282.     if (ops == 0) {
  283.     code = gs_note_error(e_VMerror);
  284.     goto fail;
  285.     }
  286.     size = 0;
  287.     check_psc_function(proc, 0, ops, &size); /* can't fail */
  288.     ops[size] = PtCr_return;
  289.     params.ops.data = ops;
  290.     params.ops.size = size + 1;
  291.     code = gs_function_PtCr_init(ppfn, ¶ms, mem);
  292.     if (code >= 0)
  293.     return 0;
  294.     /* free_params will free the ops string */
  295. fail:
  296.     gs_function_PtCr_free_params(¶ms, mem);
  297.     return (code < 0 ? code : gs_note_error(e_rangecheck));
  298. }
  299.